# coding:utf8

import configparser
import os
import requests
import re
main_hosts = []
optional_hosts = []
# 要排除的域名
exclude_domains = []


def read_config():
    a_config = configparser.ConfigParser()
    a_config.read('urls.ini')
    return a_config


def download_file(url, file_name):
    resp = requests.get(url)
    with open(file_name, 'w')as f:
        f.write(str(resp.content.decode('utf8')))


def read_exclude_domains(file_name):
    """
    从配置文件中读取要排除的域名，追加到exclude_domains里面去
    :param file_name:
    :return:
    """
    global exclude_domains
    with open(file_name, 'r')as f:
        while 1:
            line = f.readline()
            if not line:
                break
            line = line.replace('\n', '')
            exclude_domains.append(line)


def merge_hosts():
    # 最终要生成的hosts文件内容
    final_hosts = {}
    for a_hosts in (main_hosts+optional_hosts):
        with open(a_hosts, 'r')as f:
            while 1:
                line = f.readline()
                if not line:
                    break
                if len(line) < 7 or line[0] == '#':
                    continue
                node = re.split(" |\t", line)
                node2 = []
                for x in node:
                    x = x.replace('\n', '')
                    if x != '':
                        node2.append(x)
                if len(node2) != 2:
                    print('error when merge')
                    print(line)
                    return None
                it_should_be_excluded = False
                for xx in exclude_domains:
                    if node2[1].find(xx) >= 0:
                        it_should_be_excluded = True
                        break
                if not it_should_be_excluded and node2[1] not in final_hosts :
                    final_hosts[node2[1]] = node2[0]
    return final_hosts


if __name__ == '__main__':
    config = read_config()
    main_part = config['main']
    for key in main_part:
        if main_part[key].find('http') >= 0:
            # 下载地址里的hosts文件
            download_file(main_part[key], key)
        main_hosts.append(key)
    optional_part = config['optional']
    for key in optional_part:
        if optional_part[key].find('http') >= 0:
            # 下载地址里的hosts文件
            download_file(optional_part[key], key)
        optional_hosts.append(key)
    exclude_part = config['exclude']
    for key in exclude_part:
        read_exclude_domains(exclude_part[key])
    print(exclude_domains)
    write_hosts = merge_hosts()
    with open('hosts', 'w')as f:
        for key in write_hosts:
            f.write(write_hosts[key]+"  "+key+"\n")
        import socket
        f.write("127.0.0.1  "+socket.gethostname())

